/* ComicsScraper.cpp
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Function definitions for the ComicsScraper class.
*/

#include "ComicsScraper.h"

#include <sstream>

#include <fstream>

using std::ostringstream;



// Constructor:
ComicsScraper::ComicsScraper(const string &name, const string &description)
	: name(name), description(description + " (from comics.com).")
{
	SetIndex(GetLastIndex());
}



// DoClone:
// Make a copy of this Scraper.
Scraper *ComicsScraper::DoClone() const
{
	ComicsScraper *scraper = new ComicsScraper(name, "");
	scraper->description = description;
	scraper->path = path;
	
	return scraper;
}



// GetLastIndex:
// Get the index for today's date.
int ComicsScraper::GetLastIndex() const
{
	return 0;
}



// GetPath:
// Get the URL of the image for the given index.
string ComicsScraper::GetPath(int index) const
{
	return path;
}



// GetDescription:
// Get the description string to display for this comic, for this index.
string ComicsScraper::GetDescription(int index) const
{
	// An index of zero means today. Negative numbers are earlier days.
	time_t thisTime = time(NULL) + (24 * 60 * 60 * index);
	tm date = *localtime(&thisTime);
	
	return description + " Comic for " + DateToString(date) + ".";
}



// LoadIndex:
// Load the comic for the given index into the given buffer, using the 
// given internet connection.
bool ComicsScraper::LoadIndex(int index, string &buffer, Curl &curl)
{
	if(index > 0)
		return false;
	
	// Get the web page containing this day's comic.
	// An index of zero is today (page 1).
	ostringstream out;
	out << "/?Page=" << (1 - index);
	
	// Construct the URL of the HTML page.
	path = "http://comics.com/" + name + out.str();
	
	string page;
	Fetch(path, page, curl);

	size_t pathPos = page.find("STR_StripImage");
	static const string imageTag = "img src=\"";
	pathPos = page.find(imageTag, pathPos);
	if(pathPos == string::npos)
		return false;
	
	// Skip the 'img src="'.
	pathPos += imageTag.length();
	
	// Find the closing quotation mark, and extract the full image path.
	size_t pathEnd = page.find('"', pathPos);
	path.assign(page, pathPos, pathEnd - pathPos);

	Fetch(path, buffer, curl);

	// Check that the image was loaded successfully.
	if(buffer.empty())
		return false;
	
	SetIndex(index);
	
	return true;

}



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
